#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "temp_functions.h"

#define SIZE 365*24*60 /*������ ������� 365 ���� � 24 ���� � 60 �����*/

int main(int argc, char **argv)
{
    int res = 0, data = 0, flag=1;
    int month = 0;
    char *filename = "";
    while ((res = getopt(argc, argv, "hf:m:")) != -1)
    {
        switch (res)
        {
        case 'h':
            printf("This app performs statistical processing of data from the temperature sensor. Data in YYYY;MM;DD;HH;MM;TT format are taken from a file.\n");
            printf("Using: prog.exe -h -m:[0...12] -f[filename]\n");
            printf("-h - HELP\n");
            printf("-m - MONTH [0...12]\n");
            printf("-f - input data FILENAME");
            return 0;

        case 'm':
            month = atoi(optarg);
            if ((month<1)||(month>12))
            {
                printf("-m [MONTH] must be an integer in range [1...12]\n");
                return 1;
            }
            break;

        case 'f':
            filename = optarg;
            // printf("%s\n",filename);
            break;

        case '?':
            printf("This app performs statistical processing of data from the temperature sensor. Data in YYYY;MM;DD;HH;MM;TT format are taken from a file.\n");
            printf("'-h' for help\n");
            return 0;
        }
        flag=0;
    }
    if(flag)
    {
        printf("This app performs statistical processing of data from the temperature sensor. Data in YYYY;MM;DD;HH;MM;TT format are taken from a file.\n");
        printf("'-h' for help\n");
        return 0;
    }
        
    struct sensor* info=malloc(SIZE*sizeof(struct sensor)); //�������� ������� ������
    if(!info)
    {
        return -1; //���������, ��� ������ ��������
    }

    data = ReadFile(info, filename);
    if (data < 1)
    {
        free(info);
        return 0;
    }
    // print(info, data);
    if (month)
    {
        printf("===================================\n");
        printf("%2d.%4d stats:\n", month, info[0].year);
        printf("===================================\n");
        if (MaxMonth(info, data, month) == -99)
        {
            printf("  NO DATA  ");
        }
        else
        {
            printf("MAX TEMP %7d\n", MaxMonth(info, data, month));
            printf("MIN TEMP %7d\n", MinMonth(info, data, month));
            printf("MEAN TEMP %8.1f\n", MeanMonth(info, data, month));
        }
    }
    else
    {
        printf("===================================\n");
        printf("%4d year stats:\n", info[0].year);
        printf("===================================\n");
        printf("MAX TEMP %7d\n", MaxYear(info, data));
        printf("MIN TEMP %7d\n", MinYear(info, data));
        printf("MEAN TEMP %8.1f\n", MeanYear(info, data));
    }
    free(info);
    return 0;
}
